﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InTheHand.Net.Sockets;
using InTheHand.Net.Bluetooth;
using InTheHand.Net;
using System.IO;
using BluetoothTransmitter;
using BluetoothServer;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        String config;
        int duration, normalBeat;
        Int16 realInterval;
        Int16[] arrayOfInterval = new Int16[1000];
        Int16 currentPositionFromArray = 0;
        BluetoothTransmitter.Transmitter sendIntervals = new Transmitter();
        Guid serviceName = new Guid("{00001402-0000-1000-8000-00805F9B34FB}");
        BluetoothServer.CalculateIntervals calculateIntervals = new CalculateIntervals();
        InTheHand.Net.Sockets.BluetoothClient _bc = new InTheHand.Net.Sockets.BluetoothClient();
        BluetoothListener lsnr;
        Stream stream = null;

        public Form1()
        {
            InitializeComponent();
                
        }

        private void button1_Click(object sender, EventArgs e)
        {
            lauchGenerator.Text = "Генератор запущен";
            lauchGenerator.Enabled = false;
            intervalsGenerateTimer.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            duration = dispersion.Value;
            normalBeat = intervalBar.Value;
            ultimateSigma.Text = Convert.ToString(calculateIntervals.getUltimateSigmaMedian(arrayOfInterval, currentPositionFromArray, intervalBar.Value));
            realInterval = calculateIntervals.newInterval(arrayOfInterval, duration, currentPositionFromArray, intervalBar.Value);
            intervalsGenerateTimer.Interval = realInterval;
            intervals.AppendText(Convert.ToString(realInterval)+ Environment.NewLine );
            currentPositionFromArray=calculateIntervals.addInterval(arrayOfInterval, currentPositionFromArray, realInterval);
            intervalText.Text = Convert.ToString(realInterval);
            durationText.Text = Convert.ToString(duration);
            int bpm = calculateIntervals.getBPM(arrayOfInterval, currentPositionFromArray, intervalBar.Value);
            bpmText.Text = Convert.ToString(bpm);
            config = sendIntervals.requestListner();
            if (config=="config")
            {
                bool isConfigSet = setConfig();
                if (!isConfigSet)
                {
                    //Do nothing
                }
                else
                {
                    sendIntervals.okAnswer();
                }
            }
            progress.Value = currentPositionFromArray;
            if ((config=="dataRequest")||currentPositionFromArray == 999)
            {
                sendAll();
            }
        }

private bool setConfig()
    {
        int configType;
        sendIntervals.okAnswer();
        configType = sendIntervals.resiveConfigType();
        switch (configType)
        {
            case (3):
                {
                    transmitterTimer.Enabled = true;
                    transmitterTimer.Interval = 2000;
                    return true;
                }
            case (4):
                {
                    transmitterTimer.Enabled = true;
                    transmitterTimer.Interval = 4000;
                    return true;
                }
            default:
                {
                    return false;
                }
        }
    }


public void sendAll()
{

    sendIntervals.sendData(arrayOfInterval, currentPositionFromArray);
    intervals.Clear();
    currentPositionFromArray = 0;
   
}

private void button3_Click(object sender, EventArgs e)
{
    sendAll();
}

private void connectionWaiting_Click(object sender, EventArgs e)
{
     sendIntervals.connectionWait();
    connectionState.Text = "Смартфон подсоединился";

}

private void transmitterTimer_Tick(object sender, EventArgs e)
{

    sendIntervals.radio(currentPositionFromArray);
}

    }
}
